package cn.wolfcode.cms.web.controller;

import cn.wolfcode.cms.annotation.PermissionName;
import cn.wolfcode.cms.domain.DictionaryItems;
import cn.wolfcode.cms.query.QueryObject;
import cn.wolfcode.cms.service.IDictionaryItemsService;
import cn.wolfcode.cms.util.JSONResult;
import org.apache.shiro.authz.annotation.RequiresPermissions;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Controller;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.ResponseBody;

//数据字典
@Controller
@RequestMapping("dictionaryItems")
public class DictionaryItemsController {

    @Autowired
    private IDictionaryItemsService dictionaryItemsService;

    @RequiresPermissions("dictionaryItems:index")
    @PermissionName("数据字典首页")
    @RequestMapping("")
    public String index() {
        dictionaryItemsService.list();
        return "dictionaryItems/list";
    }

    //数据字典信息列表
    @RequiresPermissions("dictionaryItems:list")
    @PermissionName("数据字典列表")
    @RequestMapping("list")
    @ResponseBody
    public Object list(QueryObject qo) {
        return dictionaryItemsService.query(qo);
    }

    //通过字典的sn获取对应的明细集合
    @RequestMapping("getItemByDataDictionarySn")
    @ResponseBody
    public Object getItemByDataDictionarySn(String sn) {
        return dictionaryItemsService.getItemByDataDictionarySn(sn);
    }


    //数据字典信息列表/带分页
    @RequestMapping("selectAllSn")
    @ResponseBody
    public Object selectAllSn() {
        return dictionaryItemsService.selectAllSn();
    }


    //所有数据字典信息数据/无分页
    @RequestMapping("listData")
    @ResponseBody
    public Object listData() {
        return dictionaryItemsService.list();
    }


    //数据字典添加
    @RequiresPermissions("dictionaryItems:add")
    @PermissionName("数据字典添加")
    @RequestMapping("save")
    @ResponseBody
    public Object save(DictionaryItems entity) {
        JSONResult result = new JSONResult();
        try {
            dictionaryItemsService.save(entity);
        } catch (Exception e) {
            e.printStackTrace();
            result.mark("添加失败");
        }
        return result;
    }

    //数据字典编辑
    @RequiresPermissions("dictionaryItems:edit")
    @PermissionName("数据字典编辑")
    @RequestMapping("edit")
    @ResponseBody
    public Object edit(DictionaryItems entity) {
        JSONResult result = new JSONResult();
        try {
            dictionaryItemsService.update(entity);
        } catch (Exception e) {
            e.printStackTrace();
            result.mark("编辑失败");
        }
        return result;
    }

    //数据字典删除
    @RequiresPermissions("dictionaryItems:delete")
    @PermissionName("数据字典删除")
    @RequestMapping("delete")
    @ResponseBody
    public Object delete(Long id) {
        JSONResult result = new JSONResult();
        try {
            dictionaryItemsService.delete(id);
        } catch (Exception e) {
            e.printStackTrace();
            result.mark("删除失败");
        }
        return result;
    }
}
